#include<stdio.h>
#include<stdlib.h>
#include<conio.h> 
#include<windows.h>
#define high 25
#define width 50//边框 

#define enenum 5 
//全局变量 
int canvas[high][width]={0};//0空格，-1边框#,
                            //1蛇头@，>1身体*,-2为食物 
int score;
int movedire;//移动方向1上2下3左4右 

int food_x,food_y;//食物 

void HideCursor()         //隐藏光标
{
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};
	SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x,int y)//光标移动到x，y位置 
{
	HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X=x;
	pos.Y=y;
	SetConsoleCursorPosition(handle,pos);
}


void startup()//定义游戏初始化
{
    HideCursor();//隐藏光标 
    
	int i,j;
    //边框 
	for(i=0;i<high;i++)
    {
    	canvas[i][0]=-1;
    	canvas[i][width-1]=-1;
    }
    for(j=0;j<width;j++)
    {
    	canvas[0][j]=-1;
    	canvas[high-1][j]=-1;
    }
    
	//蛇头 
    canvas[high/2][width/2]=1;
    //身体 
	for(i=1;i<=4;i++)
         canvas[high/2][(width/2)-i] =i+1;
	//食物
	food_x=rand()%(high-5)+2;
	food_y=rand()%(width-5)+2;
	canvas[food_x][food_y]=-2;
	
	score=0;
    
    movedire=4; 
}

void movesnake()
{
	int i,j;
	int max=0;
	int oldtail_i,oldtail_j;//旧尾巴 
	int oldhead_i,oldhead_j;//旧头部 
	int newhead_i,newhead_j;//新蛇头 
	
	
	for(i=1;i<high-1;i++)
	{
	    for(j=1;j<width-1;j++)
		{	
			if(canvas[i][j]>0)
			{
			   //大于零元素加一 
			   canvas[i][j]++;
			   
			   //求最大 
			   if(max< canvas[i][j])
			   {
			   	   max=canvas[i][j];
			   	   oldtail_i=i;
			   	   oldtail_j=j;
			   }
			   //记录旧蛇头 
			   if(canvas[i][j]==2)
			   {
			   	   oldhead_i=i;
			   	   oldhead_j=j;
			   }
			}
		}
	} 
	
	if(movedire==1)//上 
	{
	    newhead_i=oldhead_i-1;
	    newhead_j=oldhead_j;
	}
	if(movedire==2)//下 
	{
	    newhead_i=oldhead_i+1;
	    newhead_j=oldhead_j;
	}
	if(movedire==3)//左 
	{
	    newhead_i=oldhead_i;
	    newhead_j=oldhead_j-1;
	}
	if(movedire==4)//右 
	{
        newhead_i=oldhead_i;
	    newhead_j=oldhead_j+1;
	}
	 
	 //判断头碰食物  
	if(canvas[newhead_i][newhead_j]==-2)
	{
		canvas[food_x][food_y]=0;//原始归零
		 
		food_x=rand()%(high-5)+2;
	    food_y=rand()%(width-5)+2;
	    canvas[food_x][food_y]=-2;
	    
	    score++;
	}
	else
	//,没吃到食物时，最大元素为0 
	canvas[oldtail_i][oldtail_j]=0;
	
	//判断头撞到或自身失败
    if(canvas[newhead_i][newhead_j]>0||canvas[newhead_i][newhead_j]==-1)
    {
    	 printf("游戏失败");
		exit(0); 
    }
    else
        canvas[newhead_i][newhead_j]=1;
        
        
} 

void show()//显示 
{
	gotoxy(0,0);
	
	int i,j;
	
	for(i=0;i<high;i++) 
	{
		for(j=0;j<width;j++)
		{
			if(canvas[i][j]==-1)//输出边框 
			    printf("#");
			else if(canvas[i][j]==1) //输出蛇头
			     printf("@"); 
			else if(canvas[i][j]==-2) //输出食物 
			     printf("F"); 
			else if(canvas[i][j]>1) //输出蛇身 
			    printf("*");
			else if(canvas[i][j]==0) 
			    printf(" ");
		}
		printf("\n");
	}

	printf("得分；%d",score);
	
	Sleep(100);
}


void updatewithoutinput()//与用户无关的输入更新，比如计分
{
	movesnake();
	
	
}

void updatewithinput()//与用户有关的输入更新
{	
    int input;
	
	if(kbhit())     //当按键时执行 
	{
		input= getch();
		
		if(input=='a')
		   movedire=3;
		if(input=='d')
		  movedire=4;
		if(input=='w')
		   movedire=1;
		if(input=='s')
		  movedire=2;
	}
}

//主函数
int main()
{
    startup();         //数据初始化 
	while(1)           //游戏循坏执行 
	{
		show();      //显示画面 
		updatewithoutinput();  //与用户输入无关的更新 
		updatewithinput();     //与用户输入有关的更新 
		 
	}  
} 